package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcCircleMsg;
import com.visizen.wisdomclass.server.model.WcSession;
import com.visizen.wisdomclass.server.model.WcSessionExample;
import com.visizen.wisdomclass.server.model.WcTalk;
import com.visizen.wisdomclass.server.model.dto.SessionWithTalkExt;
import com.visizen.wisdomclass.server.model.dto.TalkWithFullnameExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface WcSessionMapper {

	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@SelectProvider(type = WcSessionSqlProvider.class, method = "countByExample")
	int countByExample(WcSessionExample example);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@DeleteProvider(type = WcSessionSqlProvider.class, method = "deleteByExample")
	int deleteByExample(WcSessionExample example);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@Delete({ "delete from wc_session",
			"where session_id = #{session_id,jdbcType=BIGINT}" })
	int deleteByPrimaryKey(Long session_id);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@Insert({
			"insert into wc_session (session_id, organization_id, ",
			"attach_id, session_name, ",
			"createtime, state, ",
			"session_mode)",
			"values (#{session_id,jdbcType=BIGINT}, #{organization_id,jdbcType=BIGINT}, ",
			"#{attach_id,jdbcType=BIGINT}, #{session_name,jdbcType=VARCHAR}, ",
			"#{createtime,jdbcType=TIMESTAMP}, #{state,jdbcType=INTEGER}, ",
			"#{session_mode,jdbcType=INTEGER})" })
    @Options(useGeneratedKeys = true, keyProperty = "session_id", keyColumn = "session_id")
	int insert(WcSession record);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@InsertProvider(type = WcSessionSqlProvider.class, method = "insertSelective")
	int insertSelective(WcSession record);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@SelectProvider(type = WcSessionSqlProvider.class, method = "selectByExample")
	@Results({
			@Result(column = "session_id", property = "session_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "session_name", property = "session_name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "state", property = "state", jdbcType = JdbcType.INTEGER),
			@Result(column = "session_mode", property = "session_mode", jdbcType = JdbcType.INTEGER) })
	List<WcSession> selectByExample(WcSessionExample example);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@Select({
			"select",
			"session_id, organization_id, attach_id, session_name, createtime, state, session_mode",
			"from wc_session",
			"where session_id = #{session_id,jdbcType=BIGINT}" })
	@Results({
			@Result(column = "session_id", property = "session_id", jdbcType = JdbcType.BIGINT, id = true),
			@Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
			@Result(column = "session_name", property = "session_name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
			@Result(column = "state", property = "state", jdbcType = JdbcType.INTEGER),
			@Result(column = "session_mode", property = "session_mode", jdbcType = JdbcType.INTEGER) })
	WcSession selectByPrimaryKey(Long session_id);


    /**
     * 根据 两个 userId 查询出对应的session，必须是userId 完全匹配的
     * @param firstUserId
     * @param secondUserId
     * @return
     */
    @Select({"SELECT s.* " +
            "FROM wc_session_user su LEFT JOIN wc_session AS s ON s.`session_id` = su.`session_id` " +
            "WHERE su.`user_id` IN (#{firstUserId},#{secondUserId}) " +
            "AND EXISTS(SELECT 1 FROM wc_session_user WHERE user_id = #{firstUserId} AND session_id = su.`session_id`) " +
            "AND EXISTS(SELECT 1 FROM wc_session_user WHERE user_id = #{secondUserId} AND session_id = su.`session_id`) " +
            "AND (SELECT COUNT(user_id) FROM wc_session_user WHERE session_id = su.`session_id`) = 2 " +
            "AND s.`organization_id` = #{organizationId} "+
            "GROUP BY session_id"})
    @Results({
            @Result(column = "session_id", property = "session_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "organization_id", property = "organization_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "attach_id", property = "attach_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "session_name", property = "session_name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "state", property = "state", jdbcType = JdbcType.INTEGER),
            @Result(column = "session_mode", property = "session_mode", jdbcType = JdbcType.INTEGER) })
    WcSession selectItemByUserList(@Param("organizationId") Long organizationId,@Param("firstUserId") Long firstUserId,@Param("secondUserId") Long secondUserId);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcSessionSqlProvider.class, method = "updateByExampleSelective")
	int updateByExampleSelective(@Param("record") WcSession record,
			@Param("example") WcSessionExample example);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcSessionSqlProvider.class, method = "updateByExample")
	int updateByExample(@Param("record") WcSession record,
			@Param("example") WcSessionExample example);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@UpdateProvider(type = WcSessionSqlProvider.class, method = "updateByPrimaryKeySelective")
	int updateByPrimaryKeySelective(WcSession record);


	/**
	 * This method was generated by MyBatis Generator. This method corresponds to the database table wc_session
	 * @mbggenerated
	 */
	@Update({ "update wc_session",
			"set organization_id = #{organization_id,jdbcType=BIGINT},",
			"attach_id = #{attach_id,jdbcType=BIGINT},",
			"session_name = #{session_name,jdbcType=VARCHAR},",
			"createtime = #{createtime,jdbcType=TIMESTAMP},",
			"state = #{state,jdbcType=INTEGER},",
			"session_mode = #{session_mode,jdbcType=INTEGER}",
			"where session_id = #{session_id,jdbcType=BIGINT}" })
	int updateByPrimaryKey(WcSession record);


	@Insert({
			"insert into wc_session (session_id, session_name, ",
			"createtime, organization_id, ",
			"attach_id)",
			"values (#{session_id,jdbcType=BIGINT}, #{session_name,jdbcType=VARCHAR}, ",
			"#{createtime,jdbcType=TIMESTAMP}, #{organization_id,jdbcType=BIGINT}, ",
			"#{attach_id,jdbcType=BIGINT})" })
    @Options(useGeneratedKeys = true, keyProperty = "session_id", keyColumn = "session_id")
    int insertAndGenerateKey(WcSession record);


//    @Select({
//            "select t.talk_id, s.* from wc_talk t, wc_user u, wc_session s",
//            "where t.user_id = u.user_id ",
//            "and t.talk_time > #{timestamp}",
//            "and s.organization_id = #{organizationId}",
//            "order by talk_time desc limit 1"
//    })
    // TODO 这时的获取会话列表已经趋于完善，不仅返回了正确的数据，还加入了 与当前用户相关的判断，只会返回与当前用户相关的会话
    @Select("SELECT t.talk_id, s.* " +
            "FROM (SELECT * FROM wc_talk ORDER BY talk_time DESC) AS t  " +
            "LEFT JOIN wc_session AS s ON s.`session_id` = t.`session_id` " +
            "LEFT JOIN wc_user AS u ON u.`user_id` = t.`user_id` " +
            "LEFT JOIN wc_session_user AS su ON su.`session_id` = s.`session_id` " +
            "WHERE t.talk_time > #{timestamp} " +
            "AND s.`organization_id` = #{organizationId} " +
            "AND su.`user_id` IN (#{userId}) " +
            "GROUP BY t.session_id  " +
            "ORDER BY t.talk_time DESC ")
    @Results({
            @Result(property = "talk", column = "talk_id", one = @One(select = "selectTalkWithExtraByPrimaryKey")),
            @Result(column = "session_id", property = "session_id", jdbcType = JdbcType.BIGINT, id = true)
    })
    List<SessionWithTalkExt> getSessionListLastTalkAfterTimestamp(@Param("organizationId") Long organizationId,
                                                                  @Param("timestamp") Date timestamp,
                                                                  @Param("userId") Long userId);
//    @Select({
//            "select t.talk_id, s.* from wc_talk t, wc_user u, wc_session s",
//            "where t.user_id = u.user_id ",
//            "and t.talk_time < #{timestamp}",
//            "and s.organization_id = #{organizationId}",
//            "order by talk_time desc limit 1"
//    })
    @Select("SELECT t.talk_id, s.* " +
            "FROM (SELECT * FROM wc_talk ORDER BY talk_time DESC) AS t  " +
            "LEFT JOIN wc_session AS s ON s.`session_id` = t.`session_id` " +
            "LEFT JOIN wc_user AS u ON u.`user_id` = t.`user_id` " +
            "LEFT JOIN wc_session_user AS su ON su.`session_id` = s.`session_id` " +
            "WHERE t.talk_time < #{timestamp} " +
            "AND s.`organization_id` = #{organizationId} " +
            "AND su.`user_id` IN (#{userId}) " +
            "GROUP BY t.session_id  " +
            "ORDER BY t.talk_time DESC ")
    @Results({
            @Result(property = "talk", column = "talk_id", one = @One(select = "selectTalkWithExtraByPrimaryKey")),
            @Result(column = "session_id", property = "session_id", jdbcType = JdbcType.BIGINT, id = true)
    })
    List<SessionWithTalkExt> getSessionListLastTalkBeforeTimestamp(@Param("organizationId") Long organizationId,
                                                                   @Param("timestamp") Date timestamp,
                                                                   @Param("userId") Long userId);

    @Select({
            "select t.*,u.user_id as fromUserId, u.fullname as fromUserFullName, u.attach_id as fromUserAttachId from wc_talk t, wc_user u",
            "where u.user_id = t.user_id",
            "and t.talk_id = #{talk_id}"
    })
    TalkWithFullnameExt selectTalkWithExtraByPrimaryKey(@Param("talk_id") Long talk_id);
}
